#include "gps.h"
#include "image.h"
#include "mainwindow.h"
//#include "map.h"
#include "serialport.h"
#include "terminal.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initialiseMap();
    initialiseSerialPort();
    initialiseGPS();
    connect(tmn, SIGNAL(dataReceived(QString)), this, SLOT(terminalLogUpdate(QString)));
    connect(mapobject, SIGNAL(changeReceived(QEvent)), this, SLOT(retranslate(QEvent)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

//INITIALISING CLASSES
void MainWindow::initialiseMap()
{
    mapobject = new Map(ui->webView, ui->lineEdit, 0);
}

void MainWindow::initialiseSerialPort()
{
    sp = new SerialPort();
    tmn = new Terminal(sp);
}

void MainWindow::initialiseGPS()
{

}

//DEFINING BUTTONS
void MainWindow::on_actionConnect_triggered()
{
    sp->show();
}

void MainWindow::on_actionDisconnect_triggered()
{
    sp->closePort();
}

void MainWindow::on_actionExit_triggered()
{
    qApp->quit();
}

void MainWindow::on_sendButton_clicked()
{
    tmn->send(ui->inputText->text());
    ui->inputText->clear();
}

//UPDATES
void MainWindow::terminalLogUpdate(QString datastring)
{
    ui->terminalLog->append(datastring);
}

void MainWindow::retranslate(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
    ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
